<script setup>
import {onMounted, ref} from 'vue';
import {useRoute, useRouter} from 'vue-router';
import http from "@/request/http.js";
import {ElMessage} from 'element-plus';

const route = useRoute();
const router = useRouter();
const userInfo = ref({});
const articles = ref([]);
const loading = ref(true);
const articlesLoading = ref(true);

// 添加粉丝统计
const fansStatistics = ref({
  totalFans: 0,
  totalFollow: 0
})

//日期格式
const formatDate = (dateString) => {
  if (!dateString) return '未知';
  const date = new Date(dateString);
  // 处理无效日期
  if (isNaN(date.getTime())) return '未知';
  // 格式化成年月日
  return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
};

// 获取用户信息
const getUserInfo = async () => {
  try {
    loading.value = true;
    const userId = route.query.userId;
    const response = await http.get(`/user/getHomeInfo?userId=${userId}`)

    if (response.code === 200) {
      userInfo.value = response.data;
    } else {
      ElMessage.error('获取用户信息失败');
    }
  } catch (error) {
    console.error('获取用户信息出错:', error);
    ElMessage.error('网络错误，无法获取用户信息');
  } finally {
    loading.value = false;
  }
};

// 获取粉丝统计数据
const fetchFansStatistics = async () => {
  try {
    const userId = route.query.userId || '';
    const response = await http.get(`/follow/getFansStatistics?userId=${userId}`)

    if (response.code === 200) {
      fansStatistics.value = response.data;
    }
  } catch (error) {
    console.error('获取粉丝统计失败:', error);
  }
}

// 获取用户文章列表
const getUserArticles = async () => {
  try {
    articlesLoading.value = true;
    let apiUrl = '/article/getList?currentPage=1&pageSize=100';

    apiUrl += `&username=${userInfo.value.username}`;
    const response = await http.get(apiUrl);
    if (response.code === 200) {
      articles.value = response.data?.records || [];
      //计算统计数据
      const articleCount = articles.value.length;
      const likeCount = articles.value.reduce((total, article) => {
        return total + (article.likeCount || 0);
      }, 0);
      // 更新用户信息中的统计数据
      userInfo.value.articleCount = articleCount;
      userInfo.value.likeCount = likeCount;
    } else {
      ElMessage.error('获取文章列表失败');
      articles.value = [];
    }
  } catch (error) {
    console.error('获取文章列表出错:', error);
    ElMessage.error('获取文章列表失败');
    articles.value = [];
  } finally {
    articlesLoading.value = false;
  }
};

// 跳转到文章详情
const goToArticleDetail = (articleId) => {
  router.push(`/article/${articleId}`);
};

// 页面加载时获取用户信息和文章列表
onMounted(async () => {
  await getUserInfo();
  await fetchFansStatistics();
  await getUserArticles();
});

// 跳转到首页
const goToIndex = () => {
  router.push('/');
}
</script>

<template>
  <div class="profile-page">
    <!-- 顶部背景图 -->
    <div class="profile-header-banner">
      <div class="banner-pattern">
        <div class="pattern-circle pattern-1"></div>
        <div class="pattern-circle pattern-2"></div>
        <div class="pattern-circle pattern-3"></div>
        <div class="pattern-square pattern-4"></div>
        <div class="pattern-square pattern-5"></div>
        <div class="pattern-triangle pattern-6"></div>
      </div>
      <div class="banner-overlay"></div>
    </div>

    <!-- 主体内容 -->
    <div class="container">
      <!-- 头像和基本信息区 -->
      <div class="profile-card">
        <div class="avatar-container">
          <div class="avatar-wrapper">
            <img
                :class="{ 'loading': loading }"
                :src="userInfo.avatar"
                alt="用户头像"
                class="user-avatar"
            >
          </div>
        </div>

        <div class="profile-info">
          <div class="info-header">
            <h1 class="username">{{ userInfo.username || '未设置' }}</h1>
            <button
                :disabled="loading"
                class="edit-btn"
                @click="goToIndex"
            >
              回到首页
            </button>
          </div>

          <!-- 个人简介 -->
          <p class="user-bio">{{ userInfo.briefly || '该用户尚未填写个人简介' }}</p>

          <div class="user-meta">
            <div class="meta-item">
              <span class="meta-label">粉丝：</span>
              <span class="meta-value">{{ fansStatistics.totalFans || 0 }}</span>
            </div>
            <div class="meta-item">
              <span class="meta-label">关注：</span>
              <span class="meta-value">{{ fansStatistics.totalFollow || 0 }}</span>
            </div>
            <div class="meta-item">
              <span class="meta-label">注册时间：</span>
              <!-- 使用格式化函数处理注册时间 -->
              <span class="meta-value">{{ formatDate(userInfo.createTime) }}</span>
            </div>
            <div class="meta-item">
              <span class="meta-label">邮箱：</span>
              <span class="meta-value">{{ userInfo.email || '未绑定' }}</span>
            </div>
          </div>
        </div>
      </div>

      <!-- 数据统计和内容区 -->
      <div class="profile-content">
        <!-- 统计数据卡片 -->
        <div class="stats-card">
          <div class="stat-item">
            <div class="stat-value">{{ userInfo.articleCount || 0 }}</div>
            <div class="stat-label">发布文章</div>
          </div>
          <div class="stat-item">
            <div class="stat-value">{{ userInfo.likeCount || 0 }}</div>
            <div class="stat-label">获得点赞</div>
          </div>
        </div>

        <!-- 内容区域 -->
        <div class="content-section">
          <div class="section-header">
            <h2>个人内容</h2>
          </div>

          <div v-if="articlesLoading" class="loading-placeholder">
            <el-skeleton :rows="4" class="skeleton-loading"/>
          </div>

          <div v-else-if="articles.length === 0" class="content-placeholder">
            该用户还没有发布任何文章
          </div>

          <div v-else class="articles-list">
            <div
                v-for="article in articles"
                :key="article.id"
                class="article-item"
                @click="goToArticleDetail(article.articleId)"
            >
              <div class="article-main">
                <h3 class="article-title">{{ article.title || '未设置标题' }}</h3>
                <p class="article-summary">{{ article.summary || article.content?.substring(0, 100) || '暂无摘要' }}</p>
                <div class="article-meta">
                  <span class="article-date">{{ formatDate(article.createTime) }}</span>
                  <span class="article-views">浏览: {{ article.viewCount || 0 }}</span>
                  <span class="article-likes">点赞: {{ article.likeCount || 0 }}</span>
                </div>
              </div>
              <div v-if="article.cover" class="article-cover">
                <img :src="article.cover" alt="文章封面" class="cover-image">
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.profile-page {
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 50%, #e2e8f0 100%);
  min-height: 100vh;
  font-family: 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  position: relative;
  overflow: hidden;
}

/* 顶部背景 */
.profile-header-banner {
  height: 280px;
  background: linear-gradient(135deg, #a8d4f5 0%, #8bc2f0 50%, #6fb0eb 100%);
  position: relative;
  overflow: hidden;
}

.banner-pattern {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0.6;
}

/* 几何图案样式 */
.pattern-circle,
.pattern-square,
.pattern-triangle {
  position: absolute;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 8px;
}

.pattern-circle {
  border-radius: 50%;
}

.pattern-square {
  border-radius: 4px;
}

.pattern-triangle {
  width: 0;
  height: 0;
  background: transparent !important;
  border-left: 20px solid transparent;
  border-right: 20px solid transparent;
  border-bottom: 34.6px solid rgba(255, 255, 255, 0.15);
}

/* 图案位置和大小 */
.pattern-1 {
  width: 80px;
  height: 80px;
  top: 20%;
  left: 10%;
  animation: float 6s ease-in-out infinite;
}

.pattern-2 {
  width: 120px;
  height: 120px;
  top: 60%;
  left: 80%;
  animation: float 8s ease-in-out infinite 1s;
}

.pattern-3 {
  width: 60px;
  height: 60px;
  top: 30%;
  left: 85%;
  animation: float 7s ease-in-out infinite 0.5s;
}

.pattern-4 {
  width: 100px;
  height: 100px;
  top: 70%;
  left: 15%;
  animation: float 9s ease-in-out infinite 1.5s;
  transform: rotate(45deg);
}

.pattern-5 {
  width: 70px;
  height: 70px;
  top: 15%;
  left: 70%;
  animation: float 5s ease-in-out infinite 2s;
  transform: rotate(15deg);
}

.pattern-6 {
  top: 50%;
  left: 25%;
  animation: float 10s ease-in-out infinite;
}

/* 浮动动画 */
@keyframes float {
  0%, 100% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(180deg);
  }
}

.banner-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(circle at 20% 80%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
  radial-gradient(circle at 80% 20%, rgba(255, 255, 255, 0.1) 0%, transparent 50%),
  radial-gradient(circle at 40% 40%, rgba(255, 255, 255, 0.05) 0%, transparent 50%);
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  position: relative;
  top: -100px;
}

/* 个人资料卡片样式 */
.profile-card {
  background-color: #fff;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
  padding: 32px;
  display: flex;
  margin-bottom: 24px;
  transition: transform 0.3s ease, box-shadow 0.3s ease;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.profile-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.12);
}

.avatar-container {
  margin-right: 32px;
  flex-shrink: 0;
}

.avatar-wrapper {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  border: 4px solid #fff;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  overflow: hidden;
  transition: transform 0.3s ease;
  background: linear-gradient(135deg, #7bb7e7, #5a9bd8);
}

.avatar-wrapper:hover {
  transform: scale(1.05);
}

.user-avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: opacity 0.3s ease;
}

.user-avatar.loading {
  opacity: 0.6;
  filter: blur(2px);
}

.profile-info {
  flex: 1;
  padding-top: 16px;
}

.info-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.username {
  font-size: 28px;
  font-weight: 700;
  color: #2c3e50;
  margin: 0;
  background: linear-gradient(135deg, #2c3e50, #34495e);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.edit-btn {
  background: linear-gradient(135deg, #7bb7e7, #5a9bd8);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 12px 24px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  gap: 6px;
  box-shadow: 0 4px 12px rgba(123, 183, 231, 0.3);
}

.edit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(123, 183, 231, 0.4);
}

.edit-btn:active {
  transform: translateY(0);
}

.edit-btn:disabled {
  background: #c9dcee;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.user-bio {
  color: #5a6c7d;
  font-size: 16px;
  line-height: 1.6;
  margin: 0 0 24px 0;
  padding: 12px 0;
  border-bottom: 1px solid #f0f0f0;
}

.user-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 32px;
  padding-top: 8px;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.meta-label {
  color: #7f8c8d;
  font-size: 14px;
  font-weight: 500;
}

.meta-value {
  color: #2c3e50;
  font-size: 14px;
  font-weight: 600;
}

/* 统计数据卡片 */
.stats-card {
  background: linear-gradient(135deg, #fff 0%, #f8fafc 100%);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
  padding: 32px;
  margin-bottom: 24px;
  display: flex;
  justify-content: space-around;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.stat-item {
  text-align: center;
  padding: 16px;
  flex: 1;
  transition: all 0.3s ease;
  position: relative;
}

.stat-item:hover {
  transform: translateY(-5px);
}

.stat-item:hover::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(123, 183, 231, 0.1), rgba(90, 155, 216, 0.1));
  border-radius: 12px;
  z-index: -1;
}

.stat-value {
  font-size: 32px;
  font-weight: 800;
  color: #7bb7e7;
  margin-bottom: 8px;
  display: block;
  background: linear-gradient(135deg, #7bb7e7, #5a9bd8);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.stat-label {
  font-size: 14px;
  color: #5a6c7d;
  font-weight: 600;
}

/* 内容区域 */
.content-section {
  background: linear-gradient(135deg, #fff 0%, #f8fafc 100%);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
  padding: 32px;
  min-height: 300px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.section-header {
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 20px;
  margin-bottom: 24px;
}

.section-header h2 {
  font-size: 20px;
  font-weight: 700;
  color: #2c3e50;
  margin: 0;
  background: linear-gradient(135deg, #2c3e50, #34495e);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* 文章列表样式 */
.articles-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.article-item {
  display: flex;
  justify-content: space-between;
  padding: 24px;
  border-radius: 12px;
  border: 1px solid #f0f0f0;
  transition: all 0.3s ease;
  background: #fff;
  cursor: pointer;
  gap: 20px;
}

.article-item:hover {
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
  border-color: #7bb7e7;
}

.article-main {
  flex: 1;
  min-width: 0;
}

.article-title {
  font-size: 18px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 12px 0;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.article-summary {
  color: #5a6c7d;
  line-height: 1.6;
  margin: 0 0 16px 0;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.article-meta {
  display: flex;
  gap: 20px;
  font-size: 14px;
  color: #95a5a6;
}

.article-cover {
  flex-shrink: 0;
  width: 120px;
  height: 80px;
  border-radius: 8px;
  overflow: hidden;
}

.cover-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.article-item:hover .cover-image {
  transform: scale(1.05);
}

.content-placeholder {
  text-align: center;
  padding: 80px 20px;
  color: #95a5a6;
  border: 2px dashed #e0e0e0;
  border-radius: 12px;
  background: rgba(248, 250, 252, 0.6);
  transition: all 0.3s ease;
  font-size: 16px;
  font-weight: 500;
}

.content-placeholder:hover {
  border-color: #7bb7e7;
  background: rgba(123, 183, 231, 0.05);
  color: #5a9bd8;
}

.skeleton-loading {
  padding: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .profile-header-banner {
    height: 200px;
  }

  .container {
    top: -60px;
  }

  .profile-card {
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 24px;
  }

  .avatar-container {
    margin-right: 0;
    margin-bottom: 20px;
  }

  .avatar-wrapper {
    width: 120px;
    height: 120px;
  }

  .info-header {
    flex-direction: column;
    gap: 16px;
  }

  .user-meta {
    justify-content: center;
    gap: 20px;
  }

  .stats-card {
    flex-wrap: wrap;
  }

  .stat-item {
    flex: 1 1 40%;
    margin-bottom: 16px;
  }

  .stat-value {
    font-size: 28px;
  }

  .article-item {
    flex-direction: column;
  }

  .article-cover {
    width: 100%;
    height: 160px;
    order: -1;
  }

  .article-meta {
    flex-wrap: wrap;
    gap: 12px;
  }

  /* 移动端减少图案数量 */
  .pattern-2,
  .pattern-5 {
    display: none;
  }
}

@media (max-width: 480px) {
  .stats-card {
    flex-direction: column;
  }

  .stat-item {
    flex: none;
  }

  .username {
    font-size: 24px;
  }

  .user-bio {
    font-size: 14px;
  }

  .article-title {
    font-size: 16px;
  }

  .article-summary {
    font-size: 14px;
  }

  .article-meta {
    font-size: 12px;
  }
}
</style>